package com.jdf.org.controller;

import com.jdf.basic.util.AjaxResult;
import com.jdf.basic.util.PageList;
import com.jdf.org.domain.Department;
import com.jdf.org.query.DepartmentQuery;
import com.jdf.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {
    @Autowired
    private IDepartmentService iDepartmentService;
    /* 找一 */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Department findOne(@PathVariable("id")Long id){
        return iDepartmentService.findById(id);

    }
    /* 找全部 */
    @GetMapping
    @ApiOperation(value = "查询全部" )
    public List<Department> findAll(){
        return iDepartmentService.loadAll();
    }
    /*
    添加与修改
     */
    @PutMapping
    @ApiOperation(value = "添加或修改" )
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if (department.getId()==null){
                iDepartmentService.add(department);
            } else {
                iDepartmentService.update(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }
    /*
    删除
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    public AjaxResult remove(@PathVariable ("id") Long id){
        try {
            iDepartmentService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }
      /*
        高级查询
     */
    @PostMapping
    @ApiOperation(value = "高级查询" )
    public PageList<Department> gaoji(@RequestBody DepartmentQuery departmentQuery){
        return iDepartmentService.queryData(departmentQuery);
    }

    // 批量删除
    @PatchMapping
    @ApiOperation(value = "批量删除" )
    public AjaxResult pathDelte(@RequestBody List<Long> ids){
        try {
            iDepartmentService.pathDelte(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    @GetMapping("/deptTree")
    @ApiOperation(value = "级联查询" )
    public List<Department> deptTree(){
        return iDepartmentService.deptTree();
    }
}
